import { FabricObject, Control, Point, XY, type InteractiveFabricObject } from 'fabric'

declare module 'fabric' {
    interface FabricObject {
        uuid?: string;
        name?: string;
        cornerLineWidth?: number;
        [key: string]: any; // 允许其他任意属性
    }
    // 确保导出的对象具有类型化的属性
    interface SerializedObjectProps {
        uuid?: string;
        name?: string;
        cornerLineWidth?: number;
        isEditPoint?: boolean;
        /** 自定义图形类型 与该图形的真实渲染类型不一定一一对应 */
        customType?: string;
    }
    interface Rect {
        points?: XY[];
    }
    interface IText {
        backgroundBorderColor?: string;
        backgroundBorderWidth?: number;
        backgroundBorderRadius?: number;
        backgroundBorderOpacity?: number;
        backgroundBorderDashArray?: number[];
        backgroundBorderDashOffset?: number;
        backgroundBorderLineCap?: string;
        backgroundBorderLineJoin?: string;
    }
    interface Polygon {
        /** 是否允许编辑多边形点模式 */
        isEditPoint?: boolean;
        /** 某个点是否正在编辑 拖拽移动 */
        editingPoint?: boolean;
        /** 图形相对画布的绝对坐标-全局坐标 */
        globalPoints?: XY[];
        /** 当前选中点坐标 index-点的索引 */
        activePoint?: {
            /** 点相对于画布的绝对坐标-全局坐标 */
            global: XY;
            /** 点相对于图形的相对坐标-局部坐标 */
            local: XY;
            index: number | null
        };
        /** 多边形在画布上相对坐标转换为绝对坐标-全局坐标 */
        convertLocalToGlobalCoordinate?: () => XY[];
        /** 多边形在画布上的全局坐标转换为在画布上的局部相对坐标 */
        convertGlobalCoordinateToLocal?: (globalPoints?: XY[]) => XY[];
    }
    interface Control {
        cornerSize: number;
        delIndex?: number;
        selectedStrokeColor?: string;
        selectedFillColor?: string;
        lineDash?: number[];
        selected?: boolean;
        deleteControl: Control;
    }
    interface CanvasEvents {
        'object:deleted': FabricObject;
        'setSelectedProperties'?: (properties: Partial<FabricObject>)=>void;
    }
    interface Canvas {
        _editAttr: TEditProperties;
    }
    interface TEditProperties {
        uuid: string
        left?: number;
        top?: number;
        width?: number;
        height?: number;
        fill?: string;
        scaleX?: number;
        scaleY?: number;
        angle?: number;
        stroke?: string;
        strokeWidth?: number;
        activePoint?: {global: XY, local: XY, index: number | null};
        [key: string]: any; // 允许其他任意属性
    }
}

// 确保属性实际添加到序列化对象中
FabricObject.customProperties = ['name', 'uuid', 'cornerLineWidth', 'isEditPoint', 'activePoint']

export type ControlRenderingStyleOverride = Partial<
  Pick<
    InteractiveFabricObject,
    | 'cornerStyle'
    | 'cornerSize'
    | 'cornerColor'
    | 'cornerStrokeColor'
    | 'cornerDashArray'
    | 'transparentCorners'
  >
>;

export type GraphType = 'Rect' | 'Polygon' | 'Line' | 'IText' | 'Circle';

export interface LayerConfig {
	type: GraphType;
	uuid?: string;
	width?: number;
	height?: number;
	top?: number;
	left?: number;
	fontSize?: number;
	radius?: number;
	points?: XY[];
	[key: string]: any;
}

